﻿using System;
using System.Collections.Generic;
using Assets.Scripts.Net;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.Net.Loader;
using Assets.Scripts.UI.Panel;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Item
{
    /// <summary>
    /// 作业未完成的Item
    /// </summary>
    public class HWUnFinishItem:UIBase<HWUnFinishItem>
    {
        public Text SeqNumText;
        public Text TitleText;
        public Text TimeText;
        public Button HWUnFinishButton;

        private HomeWorkInfo info { get; set; }//作业的id
        protected override UIType UIType
        {
            get { return UIType.Item;}
        }

        protected override void InitOnAwake()
        {
            AutoFindComponent(transform,true);
        }

        public void Init(HomeWorkInfo hwInfo, int unFinishNum)
        {
            info = hwInfo;
            SeqNumText.text = unFinishNum.ToString();
            TitleText.text = hwInfo.Name;
            TimeText.text = "截至："+hwInfo.Time; //todo 后面具体的应该需要调整
            HWUnFinishButton.onClick.AddListener(OnHWUnFinishButtonClick);
            //判断作业时间是否过期
            DateTime homeworkTime = Convert.ToDateTime(hwInfo.Time);
            if (homeworkTime.CompareTo(DateTime.Now)<0)
            { 
                HWUnFinishButton.image.color = new Color(1, 1, 1, 0.5f);
                HWUnFinishButton.interactable = false;
            }
        }

        /// <summary>
        /// 未完成按钮的点击事件(做题)
        /// </summary>
        private void OnHWUnFinishButtonClick()
        {
            List<QueInfo> queInfos =
                NetReaderFactory.Instance().GetInfoByName(NetConstValue.HW_QueInfo + "_" + info.Id) as List<QueInfo>;
            List<AnsInfo> ansInfos =
                NetReaderFactory.Instance().GetInfoByName(NetConstValue.HW_AnsInfo + "_" + info.Id) as List<AnsInfo>;
            if (queInfos != null && ansInfos != null)//说明本地已经保存数据
            {
                ExamPanel panel= CreatePanel(PanelType.ExamPanel).GetComponent<ExamPanel>();
                panel.Init(ExamType.HomeWork, info.Name, 0, queInfos, ansInfos, info.Id, OnExamSubmitPaper);
                return;
            }
            Dictionary<string, object> dict = NetManager.Instance.GetDict();
            dict.Add("type",0); 
            dict.Add("paperid",info.Id);
            Transform tsf = CreatePanel(PanelType.LoadingLoopPanel);
            tsf.GetComponent<LoadingLoopPanel>().Init();
            NetManager.Instance.SendRequest(RequestType.QuestionList, dict, () =>
            {
                Destroy(tsf.gameObject);
                if (NetReaderFactory.Instance().NetResUrls.Count > 0)//有需要加载的资源
                {      
                    LoadingPanel loadingPanel = CreatePanel(PanelType.LoadingPanel).GetComponent<LoadingPanel>();
                    loadingPanel.Init(LoadingType.WWWLoading);
                    Global.Global.Instance.StartCoroutine(WWWLoadManager.Instance()
                        .DownLoadSaveResource(NetReaderFactory.Instance().NetResUrls, () =>
                        {
                            Exit("LoadingPanel(Clone)");
                            List<QueInfo> questions = NetReaderFactory.Instance().GetInfoByType<List<QueInfo>>(NetConstValue.HW_QueInfo + "_" + info.Id) as List<QueInfo>;
                            List<AnsInfo> answers =
                                NetReaderFactory.Instance().GetInfoByType<List<AnsInfo>>(NetConstValue.HW_AnsInfo + "_" + info.Id)
                                    as List<AnsInfo>;
                            ExamPanel panel = CreatePanel(PanelType.ExamPanel).GetComponent<ExamPanel>();
                            panel.Init(ExamType.HomeWork, info.Name, 0, questions, answers, info.Id, OnExamSubmitPaper);
                        }));
                }
                else//没有需要加载的资源
                {
                    List<QueInfo> questions = NetReaderFactory.Instance().GetInfoByType<List<QueInfo>>(NetConstValue.HW_QueInfo + "_" + info.Id) as List<QueInfo>;
                    List<AnsInfo> answers =
                        NetReaderFactory.Instance().GetInfoByType<List<AnsInfo>>(NetConstValue.HW_AnsInfo + "_" + info.Id)
                            as List<AnsInfo>;
                    ExamPanel panel = CreatePanel(PanelType.ExamPanel).GetComponent<ExamPanel>();
                    panel.Init(ExamType.HomeWork, info.Name, 0, questions, answers, info.Id, OnExamSubmitPaper);
                }               
            },failed=>{
                if (failed == "1004")
                {
                    UIManager._Instance.OtherUserLogin();
                }
                Destroy(tsf.gameObject);
            });
        }

        /// <summary>
        /// 提交试卷的回调函数
        /// </summary>
        private void OnExamSubmitPaper(float score)
        {             
            Transform tsf= CreateItem(ItemType.HWFinishItem);
            tsf.SetParent(HomeWorkPanel._Instance.Content_Finish);
            tsf.localScale=Vector3.one;
            tsf.localPosition=Vector3.zero;
            HWFinishItem item = tsf.GetComponent<HWFinishItem>();
            info.Score = score;
            info.State = 1;
            item.Init(info);
            //删除本身
            Destroy(gameObject);
        }
    }
}